﻿#pragma once
#include "Settings.g.h"


namespace winrt::NovelReader::implementation
{
	struct Settings : SettingsT<Settings>
	{
		Settings();

		double FontSize();
		void FontSize(double value);

		int32_t WhiteLines();
		void WhiteLines(int32_t value);

		hstring FilePath();
		void FilePath(hstring const& value);

		int32_t ThemeIndex();
		void ThemeIndex(int32_t value);

		int32_t ChapterIndex();
		void ChapterIndex(int32_t value);

		hstring FontName();
		void FontName(hstring const& value);

		int32_t AutoTurnPageSpeed(); 
		void AutoTurnPageSpeed(int32_t value); 
		
		double LastReadingPosition(); 
		void LastReadingPosition(double value); 

		winrt::event_token PropertyChanged(winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
		void PropertyChanged(winrt::event_token const& token) noexcept;

	private:
		winrt::Windows::Foundation::IInspectable CreateObject(int32_t value); 
		winrt::Windows::Foundation::IInspectable CreateObject(winrt::hstring const& value); 
		winrt::Windows::Foundation::IInspectable CreateObject(double value); 
		winrt::Windows::Foundation::IInspectable CreateObject(uint32_t value); 

		template<typename T>
		T GetProperty(winrt::hstring const& keyName)
		{
			auto object = m_localSettings.Values().Lookup(keyName);
			return winrt::unbox_value<T>(object);
		}

		template<typename T>
		void SetProperty(winrt::hstring const& keyName, const T& value)
		{
			auto oldeValue = GetProperty<T>(keyName); 
			if (oldeValue != value) {
				auto object = CreateObject(value); 
				m_localSettings.Values().Insert(keyName, object);
				m_propertyChanged(*this, winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventArgs(keyName)); 
			}
		}

		template<typename T>
		void SetDefaultValue(winrt::hstring const& keyName, const T& value)
		{
			m_defaultSettings[keyName] = CreateObject(value); 
		}

		winrt::Windows::Storage::ApplicationDataContainer m_localSettings{ nullptr };
		winrt::event<winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
		std::map<winrt::hstring, winrt::Windows::Foundation::IInspectable> m_defaultSettings; 
	};
}
namespace winrt::NovelReader::factory_implementation
{
	struct Settings : SettingsT<Settings, implementation::Settings>
	{
	};
}

